﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CommonSense.Services.Interfaces;
using CommonSense.UI.Shell.Services;
using CommonSense.UI.Shell.ViewModels;
using CommonSense.UI.MVVM.ViewModels;
using CommonSense.UI.Shell.Interfaces;

namespace CommonSense.UI.Startup.ViewModels
{
    public class StartupNavigationViewModel : BaseViewModel
    {
        private IViewModelFactory viewModelFactory;
        private IWorkspaceService workspaceService;

        public RelayCommand SaveCommand
        {
            get;
            private set;
        }

        public RelayCommand RefreshCommand
        {
            get;
            private set;
        }

        public RelayCommand Test1Command
        {
            get;
            private set;
        }

        public RelayCommand Test2Command
        {
            get;
            private set;
        }

        public StartupNavigationViewModel(IWorkspaceService workspaceService, IViewModelFactory viewModelFactory)
        {
            this.viewModelFactory = viewModelFactory;
            this.workspaceService = workspaceService;
            this.workspaceService.ActiveViewModelChanged += this.RefreshCommands;
            this.workspaceService.ActiveViewModelDirtyChanged += this.RefreshCommands;
            this.SaveCommand = new RelayCommand(this.CanSave, this.Save);
            this.RefreshCommand = new RelayCommand(this.CanRefresh, this.Refresh);
            this.Test1Command = new RelayCommand(this.Test1);
            this.Test2Command = new RelayCommand(this.Test2);
        }

        private bool CanSave()
        {
            ISaveable saveableModel = this.workspaceService.ActiveViewModel as ISaveable;
            return saveableModel != null && saveableModel.IsDirty;
        }

        private void Save()
        {
            ISaveable saveableModel = this.workspaceService.ActiveViewModel as ISaveable;
            if (saveableModel != null)
            {
                saveableModel.Save();
            }
        }

        private bool CanRefresh()
        {
            return this.workspaceService.ActiveViewModel != null && this.workspaceService.ActiveViewModel is IRefreshable;
        }

        private void Refresh()
        {

        }

        private void Test1()
        {
            Test1ViewModel test1ViewModel = this.viewModelFactory.Create<Test1ViewModel>();
            this.workspaceService.OpenView(test1ViewModel);
        }

        private void Test2()
        {
            Test2ViewModel test2ViewModel = this.viewModelFactory.Create<Test2ViewModel>();
            this.workspaceService.OpenView(test2ViewModel);
        }

        private void RefreshCommands(object sender, EventArgs e)
        {
            this.SaveCommand.NotifyCanExecuteChanged();
            this.RefreshCommand.NotifyCanExecuteChanged();
            this.Test1Command.NotifyCanExecuteChanged();
            this.Test2Command.NotifyCanExecuteChanged();
        }
    }
}
